package com.coinland.study.view.download;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * 类描述:
 * 创建人:  shenZ
 * 创建时间: 2020/4/14
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/4/14
 */
public class DownloadClient {
    private String baseUrl = "https://www.baidu.com/";
    private static final int DEFAULT_TIMEOUT = 15;

    private ApiDownloadService downloadService;

    private JsDownloadListener mDownloadListener;

    public DownloadClient(JsDownloadListener listener) {
        mDownloadListener = listener;
    }


    public ApiDownloadService getApi() {
        JsDownloadInterceptor mInterceptor = new JsDownloadInterceptor(mDownloadListener);
        //初始化一个client,不然retrofit会自己默认添加一个
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置拦截器
                .addInterceptor(mInterceptor)
                .retryOnConnectionFailure(true)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                //设置网络请求的Url地址
                .baseUrl(baseUrl)
                //设置网络请求适配器，使其支持RxJava与RxAndroid
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        //创建—— 网络请求接口—— 实例
        downloadService = retrofit.create(ApiDownloadService.class);
        return downloadService;
    }

}
